package com.sky.directdeal.utils;

import java.util.List;

import android.app.Activity;
import android.location.Location;
import android.widget.Toast;

import com.google.android.gms.maps.model.LatLng;
import com.sky.directdeal.R;
import com.sky.directdeal.constants.Constants;
import com.sky.directdeal.listener.AddressStatusListener;
import com.sky.directdeal.listener.ServiceResponseListener;
import com.sky.directdeal.manager.ServiceManager;
import com.sky.directdeal.model.LocationAddress;
import com.sky.local_ads.geocoder.FetchGeocoderResponse;
import com.sky.local_ads.geocoder.GeoAddress;
import com.sky.local_ads.geocoder.Geometry;
import com.sky.local_ads.geocoder.LocationPoint;


public class GeoCoder {
	public static final int GETLOCATIONMSG = 1;
	public static final int GETADDRESSMSG = 2;
	private final Activity context;
	private final AddressStatusListener addressStatusListener;

	public GeoCoder(Activity context,
			AddressStatusListener addressStatusListener) {
		this.context = context;
		this.addressStatusListener = addressStatusListener;
	}

	/*this function get the lat lng from the given address by making service call*/
	public void getAddress(String address) {
		if (address != null) {
			if (addressStatusListener != null) {
				addressStatusListener.startProgress();
			}
            ServiceManager.getGecoderAddress(context, null, geoCoderServiceListener);
		} else {
			Location location =Utils.getLastKnownLocation(context);
			if (location == null) {
				Toast.makeText(
						context,
						context.getString(R.string.unable_to_determine_your_location),
						Toast.LENGTH_LONG).show();
				addressStatusListener.onFailure();
				return;
			}


			if (addressStatusListener != null) {
				addressStatusListener.startProgress();
			}

			 ServiceManager.getGecoderAddress(context, location, geoCoderServiceListener);
		}
		
	}

	
	/*public void getCustomLocationAddress(String address){
		checkInternetConnection();
		getAddress(address);
		
	}*/

	/*private void showAlert(String title, String message,
			String possitiveBtnName,
			DialogInterface.OnClickListener possitiveBtnOnClickListener,
			String negativeBtnName,
			DialogInterface.OnClickListener negativeBtnOnClickListener) {
		
		AEODialogDetails dialogDetails = new AEODialogDetails();
		
		if (title != null) {
			dialogDetails.setDialogTitle(title);
		}
		
		if (message != null) {
			dialogDetails.setDialogMessage(message);
		}
		
		ArrayList<AEOButton> buttonList = new ArrayList<AEOButton>();
		
		if (possitiveBtnName != null) {
			AEOButton button = new AEOButton(possitiveBtnName);
			button.setOnClickListener(possitiveBtnOnClickListener);
			buttonList.add(button);
		}
		
		if (negativeBtnName != null) {
			AEOButton button = new AEOButton(negativeBtnName, AEOButton.RIGHT);
			button.setOnClickListener(negativeBtnOnClickListener);
			buttonList.add(button);
		}
		
		dialogDetails.setDialogButton(buttonList);
		AeoAlertDialog gpsAlertDialog = new AeoAlertDialog(context,
				dialogDetails);
		gpsAlertDialog.setCancelable(false);
		gpsAlertDialog.show();
	}*/

	/*this listener fetch the address from the geo coder service call*/
	private final ServiceResponseListener<FetchGeocoderResponse> geoCoderServiceListener = new ServiceResponseListener<FetchGeocoderResponse>() {

		@Override
		public void onSuccess(FetchGeocoderResponse response) {

			if (response != null) {
				LocationAddress locationAddress=new LocationAddress();
				//String city = null, state = null, postalCode = null, country = null, universalCountryCode = Constants.EMPTY, subLocality1=null, subLocality2=null;
				boolean subLocality2CodeValue = false;
				fetchGeocoder: {
					int resultSetSize = response.getResultsList().size();
					if (resultSetSize == 0) {
						addressStatusListener.onFailure();
					}
					for (int i = 0; i < resultSetSize; i++) {
						int addressComponentSize = response.getResultsList()
								.get(i).getAddressComponents().size();
						
						for (int j = 0; j < addressComponentSize; j++) {
							
							List<String> locTypeList = response.getResultsList()
									.get(i).getAddressComponents().get(j)
									.getTypes();
							GeoAddress geoAddressList=response.getResultsList().get(i).getAddressComponents().get(j);
							
							if (locTypeList != null && locTypeList.size() > 0
									&& geoAddressList != null) {
								
								for (int k = 0; k < locTypeList.size(); k++) {
									
									String locaType=locTypeList.get(k);
									
									if (locaType
											.toString()
											.trim()
											.equalsIgnoreCase(
													Constants.POSTAL_CODE_TEXT)) {
										
										String postalCode = response.getResultsList()
												.get(i).getAddressComponents()
												.get(j).getLongName();
										locationAddress.setPincode(postalCode);
										
									} else if (locaType
											.toString()
											.trim()
											.equalsIgnoreCase(
													Constants.ADMINISTRATIVE_AREA_LEVEL_TEXT)) {
										
										String state = geoAddressList.getLongName();
										locationAddress.setState(state);
										
									} else if (locaType
											.toString()
											.trim()
											.equalsIgnoreCase(
													Constants.LOCALITY_TEXT)) {
										String city = response.getResultsList().get(i)
												.getAddressComponents().get(j)
												.getLongName();
										locationAddress.setLocality(city);
									}
									else if (locaType
											.toString()
											.trim()
											.equalsIgnoreCase(
													Constants.SUB_LOCALITY_1)) {
										String subLocality1 = response.getResultsList()
												.get(i).getAddressComponents()
												.get(j).getLongName();
										locationAddress.setSubLocality1(subLocality1);
									}
									else if (locaType
											.toString()
											.trim()
											.equalsIgnoreCase(
													Constants.SUB_LOCALITY_2)) {
										String subLocality2 = response.getResultsList()
												.get(i).getAddressComponents()
												.get(j).getLongName();
										locationAddress.setSubLocality2(subLocality2);
										subLocality2CodeValue=true;
									}

									else if (locaType
											.toString()
											.trim()
											.equalsIgnoreCase(
													Constants.COUNTRY_TEXT)) {
										String country = response.getResultsList()
												.get(i).getAddressComponents()
												.get(j).getLongName();
										locationAddress.setCountry(country);
										String universalCountryCode = response
												.getResultsList().get(i)
												.getAddressComponents().get(j)
												.getShortName();
										locationAddress.setUniversalCountryCode(universalCountryCode);
									}
								}
							}
						}
						
						 Geometry geometry=response.getResultsList()
									.get(i).getGeometry();
						 if(geometry!=null){
							 LocationPoint locationPoint=geometry.getLocation();
							 if(locationPoint!=null){
								 LatLng latLng=new LatLng(locationPoint.getLatitude(), locationPoint.getLongitude());
								 locationAddress.setLatLng(latLng);
							 }
						 }
						
						if (subLocality2CodeValue) {
							break fetchGeocoder;
						}
					}
				}

				if (addressStatusListener != null) {
					LocationProvider.getInstance().setLocationAddress(locationAddress);
					addressStatusListener.onSuccess(locationAddress);
				} else {
					addressStatusListener.onFailure();
				}
				
			}
		}

		@Override
		public void onFailure(Throwable throwable, String errorResponse) {
			addressStatusListener.onFailure();
			Toast.makeText(context, context.getString(R.string.geo_coder_current_loc_zip_code_failed), Toast.LENGTH_SHORT).show();

		}
	};
	
}